package com.frxuexi.report

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import com.frxuexi.report.intent.ReportIntent
import com.frxuexi.report.viewmodel.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.Date

@Composable
fun CommitReportPage(
    navHostController: NavHostController, json: String,
    vm: ReportViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {
    val entity = Gson().fromJson<ReportTypeEntity>(json, ReportTypeEntity::class.java)

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess->{
                        if(it.type==RequestType.COMMIT){
                            //刷新列表
                            navHostController.popBackStack()
                            vm.sendIntent(ReportIntent.getReports(mapOf(Pair("userId", 1), Pair("page", 1))))
                        }
                    }
                    is UiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }
        Log.i("===", entity.toString())
    })

    var now_value by remember {
        mutableStateOf("")
    }
    var next_value by remember {
        mutableStateOf("")
    }

    var isShow by remember {
        mutableStateOf(true)
    }
    if (entity.type == 4 || entity.type == 5 || entity.type == 6) {
        isShow = false
    }
    val nowtitle =
        if (entity.type == 1) "今日" else if (entity.type == 2) "本周" else "本月"
    val nexttitle =
        if (entity.type == 1) "明日" else if (entity.type == 2) "下周" else "下月"
    Column {
        BaseTitle(navHostController = navHostController, title = "工作汇报")
        Column(Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            if (isShow) {
                Text(text = entity.title)

                Text(text = "$nowtitle 工作总结")
            }


            OutlinedTextField(
                value = now_value,
                onValueChange = { now_value = it },
                Modifier.fillMaxWidth()
            )

            if (isShow) {

                Text(text = "$nexttitle 工作计划")
            }

            OutlinedTextField(
                value = next_value,
                onValueChange = { next_value = it },
                Modifier.fillMaxWidth()
            )

            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                val map = mutableMapOf<String, Any>()
                map.put("reportType",entity.type)
                map.put("reportTime",Date().time)
                map.put("reportRemind","王先生")
                map.put("reportNowTitle",nowtitle)
                map.put("reportNowValue",now_value)
                map.put("reportNextTitle",nexttitle)
                map.put("reportNextValue",next_value)
                vm.sendIntent(ReportIntent.addReport(map))
            }) {
                Text(text = "提交汇报")
            }
        }

    }

}